﻿/**
* QSmart Andon
*
* 2012-10-10
* 
* steven.li
*
* Copyright(c) 2012 All Rights Reserved. Taiyou Tech(GUANGZHOU) Co.,Ltd.  (广州太友科技) [ sales@gztaiyou.com]. 
*
*/

using System.Linq;
using System.Management;
using System.Net;


namespace FX.CC.Andon.Utility.Common
{
    public sealed class ComputerInfo
    {
        /// <summary>
        /// IP地址
        /// </summary>
        /// <returns></returns>
        public static string GetIpAddress()
        {
#pragma warning disable 612,618
            var addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
#pragma warning restore 612,618
            return addressList[0].ToString();
        }

        /// <summary>
        /// Mac地址
        /// </summary>
        /// <returns></returns>
        public static string GetMacAddress()
        {
            var macAddress = string.Empty;
            var moc3 = GetValue("Win32_NetworkAdapterConfiguration");
            foreach (var mo in moc3.Cast<ManagementObject>().Where(mo => (bool)mo["IPEnabled"]))
            {
                macAddress = mo["MacAddress"].ToString();
                mo.Dispose();
            }
            return macAddress;
        }

        /// <summary>
        /// 硬盘序列号
        /// </summary>
        /// <returns></returns>
        public static string GetHdNo()
        {
            var hdNo = string.Empty;
            var moc2 = GetValue("Win32_DiskDrive");
            foreach (ManagementObject mo in moc2)
            {
                hdNo = (string)mo.Properties["Model"].Value;
            }
            return hdNo;
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string GethostName()
        {
            return Dns.GetHostName();
        }

        public static ManagementObjectCollection GetValue(string value)
        {
            var cimobject = new ManagementClass(value);
            ManagementObjectCollection moc = cimobject.GetInstances();
            return moc;
        }
    }
}
